package com.tushu;


import com.tushu.dao.*;
import com.tushu.model.Admin;
import com.tushu.model.Book;
import com.tushu.model.Dto;
import com.tushu.model.Student;
import com.tushu.service.LendService;
import com.tushu.service.ReturnService;
import com.tushu.service.SearchService;

import java.util.List;
import java.util.Scanner;

public class Login {
    //当前用户
    private static Student SS;
    private static AdminDao adminDao = new AdminDao();
    private static StudentDao studentDao=new StudentDao();
    private static BookDao bookDao = new BookDao();
    private  static LentBookDao lentBookDao = new LentBookDao();
    static Scanner scanner = new Scanner(System.in);

    //书籍管理页面
    public static void bookManage() {
        System.out.println("----------------");
        System.out.println("1.所有图书信息");
        System.out.println("2.添加图书信息");
        System.out.println("3.删除图书信息");
        System.out.println("4.修改图书信息");
        System.out.println("5.返回");
        System.out.println("----------------");
        System.out.println("请选择:");
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        switch (i) {
            case 1:
                List<Book> bookList = bookDao.getBookList();
                System.out.println("id"+"\t\t"+"isbn"+"\t\t"+"书名"+"\t\t"+"作者"+"\t\t"+
                        "出版社"+"\t\t"+"馆藏数量");
                for (Book book : bookList) {
                    System.out.println(book.getId()+"\t\t"+book.getIsbn()+"\t\t"+book.getBookName()+"\t\t"+book.getAuthor()+"\t\t"+
                            book.getPublisher()+"\t\t"+book.getNum());
                }
                System.out.println("------------");
                bookManage();
                break;
            case 2:
                System.out.println("----请输入新图书信息----");
                Book book = new Book();
                Scanner scanner1=new Scanner(System.in);
                System.out.println("ISBN：");
                book.setIsbn(scanner1.nextLine());
                System.out.println("书名：");
                book.setBookName(scanner1.nextLine());
                System.out.println("作者：");
                book.setAuthor(scanner1.nextLine());
                System.out.println("出版社:");
                book.setPublisher(scanner1.nextLine());
                System.out.println("馆藏数量：");
                book.setNum(scanner1.nextInt());
                int i2 = bookDao.addBook(book);
                if (i2 > 0) {
                    System.out.println("图书添加成功！");
                    bookManage();
                } else {
                    System.out.println("图书添加失败！请稍候重试！");
                    bookManage();
                }
                break;
            case 3:
                System.out.println("请输入要删除的图书的isbn:");
                Scanner ss = new Scanner(System.in);
                String isbn = ss.nextLine();
                int i1 = bookDao.deleteBook(isbn);
                if (i1 > 0) {
                    System.out.println("图书删除成功！");
                    bookManage();
                } else {
                    System.out.println("操作失败！！！请重试！！！");
                    bookManage();
                }
            case 4:
                System.out.println("----修改图书信息----");
                System.out.println("请输入要修改图书的ISBN码：");
                Scanner scanner2 = new Scanner(System.in);
                String s = scanner2.nextLine();
                Book book2 = bookDao.getBookByIsbn(s);
                if (book2 == null) {
                    System.out.println("馆藏没有该图书信息！");
                    bookManage();
                } else {
                    System.out.println("新ISBN：");
                    book2.setIsbn(scanner2.nextLine());
                    System.out.println("新书名：");
                    book2.setBookName(scanner2.nextLine());
                    System.out.println("新作者：");
                    book2.setAuthor(scanner2.nextLine());
                    System.out.println("新出版社:");
                    book2.setPublisher(scanner2.nextLine());
                    System.out.println("新馆藏数量：");
                    book2.setNum(scanner2.nextInt());
                    int i3 = bookDao.updateBook(book2);
                    if (i3 > 0) {
                        System.out.println("图书信息-修改成功！");
                        bookManage();
                    } else {
                        System.out.println("修改失败！请稍候重试！");
                        bookManage();
                    }
                }
            case 5:
                adminMenu();
        }
    }

    //学生菜单
    public static void StudentMenu() {
        System.out.println("---------------");
        System.out.println("1.借书");
        System.out.println("2.还书");
        System.out.println("3.检索");
        System.out.println("4.退出");
        System.out.println("---------------");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择:");
        int i = scanner.nextInt();
        switch (i) {
            case 1:
                LendService.lendBook(SS);
                break;
            case 2:
                ReturnService.returnBook(SS.getId());
                break;
            case 3:
                SearchService.searchBook();
                break;
            case 4:
                view();
            default:
                System.out.println("输入错误！");
                adminMenu();
        }
    }
    //学生管理页面
    public static void studentManage() {
        System.out.println("---------------");
        System.out.println("1.所有学生信息");
        System.out.println("2.查询学生信息");
        System.out.println("3.修改学生信息");
        System.out.println("4.删除学生信息");
        System.out.println("5.添加学生信息");
        System.out.println("6.返回");
        System.out.println("----------------");
        System.out.println("请选择:");
        int i = scanner.nextInt();
        switch (i) {
            case 1:
                System.out.println("所有学生信息如下:");
                List<Student> studentList = studentDao.getStudentList();
                System.out.println("id"+"\t"+"账号"+"\t\t"+"姓名");
                for (Student student : studentList) {
                    System.out.println(student.getId() + "\t" + student.getAccount() + "\t\t" + student.getName());
                }
                studentManage();
                break;
            case 2:
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入学生账号：");
                String account = sc.nextLine();
                Student student3 = studentDao.getStudentByAccount(account);
                System.out.println("查询结果如下：");
                System.out.println("---------------");
                if (student3 != null) {
                    System.out.println("id" + "\t" + "账号" + "\t\t" + "姓名");
                    System.out.println(student3.getId() + "\t" + student3.getAccount() + "\t\t" + student3.getName());
                    studentManage();
                } else {
                    System.out.println("查无此人！！！");
                    studentManage();
                }
            case 3:
                System.out.print("请输入要修改的学生账号：");
                Scanner ss=new Scanner(System.in);
                String account3 = ss.nextLine();
                Student student = studentDao.getStudentByAccount(account3);
                if (student == null) {
                    System.out.println("该学生不存在！");
                    studentManage();
                } else {
                    System.out.println("请输入该学生新账号：");
                    Scanner scanner = new Scanner(System.in);
                    String newAccount = scanner.nextLine();
                    System.out.println("请输入该学生新密码：");
                    String newPassword = scanner.nextLine();
                    System.out.println("请输入该学生新名字：");
                    String newName = scanner.nextLine();
                    student.setAccount(newAccount);
                    student.setPassword(newPassword);
                    student.setName(newName);
                    int updateStudent = studentDao.updateStudent(student);
                    if (updateStudent > 0) {
                        System.out.println("学生信息修改成功！");
                        studentManage();
                    } else {
                        System.out.println("信息修改失败！请再次尝试");
                        studentManage();
                    }
                }

            case 4:
                System.out.println("请输入要删除的学生账号：");
                Scanner sss=new Scanner(System.in);
                String account1 = sss.nextLine();
                Student student1 = studentDao.getStudentByAccount(account1);
                if (student1 == null) {
                    System.out.println("该学生不存在！");
                    studentManage();
                } else {
                    int i1 = studentDao.deleteStudent(account1);
                    if (i1 > 0) {
                        System.out.println("成功删除账号：" + account1);
                        studentManage();
                    } else {
                        System.out.println("删除失败！请再次重试！！！");
                        studentManage();
                    }
                }
            case 5:
                System.out.println("---添加学生信息---");
                System.out.println("请输入学生账号：");
                Scanner s3 = new Scanner(System.in);
                Student student2 = new Student();
                student2.setAccount(s3.nextLine());
                System.out.println("请输入学生账号密码：");
                student2.setPassword(s3.nextLine());
                System.out.println("请输入学生姓名：");
                student2.setName(s3.nextLine());
                int i1 = studentDao.addStudent(student2);
                if (i1 > 0) {
                    System.out.println("添加学生信息成功!!");
                    studentManage();
                } else {
                    System.out.println("添加学生信息失败! 请重试");
                    studentManage();
                }
            case 6:
                adminMenu();
                break;
            default:
                System.out.println("输入错误！");
                studentManage();
        }
    }
    //管理员菜单
    public static void adminMenu(){
        System.out.println("--------------");
        System.out.println("1.学生管理");
        System.out.println("2.书籍管理");
        System.out.println("3.图书借阅一览");
        System.out.println("4.退出");
        System.out.println("--------------");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择:");
        int i = scanner.nextInt();
        switch (i) {
            case 1:
                studentManage();
                break;
            case 2:
                bookManage();
                break;
            case 3:
                List<Dto> dtos = lentBookDao.findAll();
                System.out.println("学生账号\t\t" + "学生姓名\t\t" + "书ISBN\t\t" + "书名\t\t" + "借出时间\t\t" + "应还时间\t\t");
                for (Dto dto : dtos) {
                    System.out.println(dto.getStudentAccount()+"\t\t"+
                            dto.getStudentName()+"\t\t"+
                            dto.getIsbn()+"\t\t"+
                            dto.getBookName()+"\t\t"+
                            dto.getLendTime()+"\t\t"+
                            dto.getReturnTime());
                }
                System.out.println("---------------");
                adminMenu();
            case 4:
                view();
            default:
                System.out.println("输入错误！");
                adminMenu();
        }
    }
    //管理员身份验证页
    public static void login2(){
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入账号：");
        String account = scanner.nextLine();
        System.out.print("请输入密码：");
        String password = scanner.nextLine();
        System.out.println();
    //    身份验证
        Admin admin = adminDao.selectAdmin(account);
        if (admin != null) {
            if (admin.getPassword().equals(password)) {
                System.out.println("登录成功！");
                adminMenu();
            } else {
                System.out.println("账号或者密码错误！");
                view();
            }
        }else {
            System.out.println("账号不存在！");
            view();
        }
    }
    //学生身份验证
    public static void login1(){
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入账号：");
        String account = scanner.nextLine();
        System.out.print("请输入密码：");
        String password = scanner.nextLine();
        System.out.println();
        //    身份验证
        Student student = studentDao.getStudentByAccount(account);
        if (student != null) {
            if (student.getPassword().equals(password)) {
                System.out.println("登录成功！");
                SS = student;
                StudentMenu();
            } else {
                System.out.println("账号或者密码错误！");
                view();
            }
        }else {
            System.out.println("账号不存在！");
            view();
        }
    }
    //系统首页
    public static void view() {
        while (true) {
            System.out.println("-----------------");
            System.out.println("   图书管理系统     ");
            System.out.println("   1.学生登录       ");
            System.out.println("   2.管理员登录     ");
            System.out.println("-------------------");
            System.out.print("请输入身份：");
            Scanner scanner = new Scanner(System.in);
            int option = scanner.nextInt();
            if (option == 1) {
                login1();
                break;
            } else if (option == 2) {
                login2();
                break;
            } else {
                System.out.println("输入错误");
            }
        }
    }
    public static void main(String[] args) {
        view();
    }
}
